package linear;

import java.util.Iterator;

public class Stack<T> implements Iterable<T> {
    private int N;
    private Node head;

    public Stack() {
        head = new Node(null, null);
        N = 0;
    }

    public boolean isEmpty() {
        return N == 0;
    }

    public int size() {
        return N;
    }

    public void push(T t) {
        Node oldNext = head.next;
        Node node = new Node(t, oldNext);
        head.next = node;
        N++;
    }

    public T pop() {
        Node oldNext = head.next;
        if (oldNext == null) {
            return null;
        }
        head.next = head.next.next;
        N--;
        return oldNext.item;
    }

    @Override
    public Iterator<T> iterator() {
        return new SIterator();
    }

    private class SIterator implements Iterator<T> {
        Node n;

        SIterator() {
            n = head;
        }

        @Override
        public boolean hasNext() {
            return n.next != null;
        }

        @Override
        public T next() {
            n = n.next;
            return n.item;
        }
    }

    private class Node {
        public T item;
        public Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }


    public static void main(String[] args) {
        Stack<String> stack = new Stack<>();
        stack.push("a");
        stack.push("b");
        stack.push("c");
        stack.push("d");
        for (String str : stack) {
            System.out.print(str + " ");
        }
        System.out.println("-----------------------------");
        String result = stack.pop();
        System.out.println("弹出了元素：" + result);
        System.out.println(stack.size());

        // 判断括号是否成对出现
        String str = "(上海(长安)())";
        boolean match = isMatch(str);
        System.out.println(str + "中的括号是否匹配：" + match);


        // 表达式计算
        //中缀表达式3*（17-15）+18/6的逆波兰表达式如下
        String[] notation = {"3", "17", "15", "-", "*", "18", "6", "/", "+"};
        int res = caculate(notation);
        System.out.println("逆波兰表达式的结果为：" + res);
    }

    private static int caculate(String[] notation) {
        Stack<Integer> stack = new Stack<>();
        Integer o1;
        Integer o2;
        Integer result;
        for (String item : notation) {
            switch (item) {
                case ("+"):
                    o1 = stack.pop();
                    o2 = stack.pop();
                    result = o2 + o1;
                    stack.push(result);
                    break;
                case ("-"):
                    o1 = stack.pop();
                    o2 = stack.pop();
                    result = o2 - o1;
                    stack.push(result);
                    break;
                case ("*"):
                    o1 = stack.pop();
                    o2 = stack.pop();
                    result = o2 * o1;
                    stack.push(result);
                    break;
                case ("/"):
                    o1 = stack.pop();
                    o2 = stack.pop();
                    result = o2 / o1;
                    stack.push(result);
                    break;
                default:
                    stack.push(Integer.valueOf(item));
            }
        }
        return Integer.valueOf(stack.pop());

    }

    private static boolean isMatch(String str) {
        Stack<String> stack = new Stack<>();

        for (int i = 0; i < str.length(); i++) {
            String currentChar = str.charAt(i) + "";
            if (currentChar.equals("(")) {
                stack.push(currentChar);
            }
            if (currentChar.equals(")")) {
                String t = stack.pop();
                if (t == null) {
                    return false;
                }
            }
        }
        return stack.size() == 0;
    }

}
